package cn.jia.rabbitmq.producer;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: jia
 * @Descirption:
 * @Date: 2018/4/20 11:25
 * @Modified By:
 */

@RunWith(value = SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring-context-rabbitmq.xml"})
public class SpringRabbitmqTest {
    @Autowired
    private RabbitProducerImpl rabbitProducer;

    @Test
    public void springRabbtimqTest() throws IOException {
        Map<String, Object> msg = new HashMap<String, Object>();
        msg.put("hello", "rabbitMQ");
        while (true) {
            rabbitProducer.sendMessage("spring.queue.exchange","myQueue.key", msg);
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            }
        }

    }

    /**
     * 按照模式匹配发送消息
     * @throws IOException
     */
    @Test
    public void springRabbtimqTest1() throws IOException {
        Map<String, Object> msg = new HashMap<String, Object>();
        msg.put("hello", "rabbitMQ");
        while (true) {
            //使用topic交换机进行模式匹配
            rabbitProducer.sendMessage("spring.queue.topicExchange2","myQueue.1.2", msg);
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            }
        }

    }

    /**
     * 按照模式匹配发送消息
     * @throws IOException
     */
    @Test
    public void springRabbtimqTest12() throws IOException {
        Map<String, Object> msg = new HashMap<String, Object>();
        msg.put("hello", "rabbitMQ");
        while (true) {
            //使用topic交换机进行模式匹配
            rabbitProducer.sendMessage("spring.queue.topicExchange2","m.1", msg);
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            }
        }

    }

    /**
     * 以广播形式发送消息
     */
    @Test
    public void springRabbtimqTest13() throws IOException {
        Map<String, Object> msg = new HashMap<String, Object>();
        msg.put("hello", "rabbitMQ");
        while (true) {
            //使用topic交换机进行模式匹配
            //路由名称随意填写
            rabbitProducer.sendMessage("spring.queue.fanoutExchange","m", msg);
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            }
        }

    }

}
